<template>
  <div class="page-header" :style="{background:ghost?'transparent':'#fff'}">
    <a-breadcrumb class="page-header-breadcrumb">
      <a-breadcrumb-item><home-outlined/> 管理首页</a-breadcrumb-item>
      <a-breadcrumb-item v-for="item in breadcrumbs">{{ item.breadcrumbName}}</a-breadcrumb-item>
    </a-breadcrumb>
    <div class="page-header-heading">
      <div class="page-header-heading-left">
        <span class="page-header-heading-icon" v-if="back" @click="goBack()"><arrow-left-outlined/></span>
        <span class="page-header-title">{{ title || $route.meta.title }}</span>
        <span class="page-header-sub-title">{{ subTitle }}</span>
      </div>
      <div class="page-header-heading-extra" v-if="$slots.extra"><slot name="extra"/></div>
    </div>
    <div class="page-header-content" v-if="$slots.default"><slot/></div>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue';
import { useRoute } from 'vue-router';
import { HomeOutlined,ArrowLeftOutlined } from '@ant-design/icons-vue';
import router from '@/router';

const route = useRoute();
const breadcrumbs = ref<{breadcrumbName: string; path: string }[]>([]);
defineProps({
  title:{type:String, default:''},
  subTitle:{type:String, default:''},
  ghost:{ type:Boolean, default:false },
  back:{ type:Boolean, default:false },
})

watch(
  () => route.path,
  () => {
    const matchedRoutes = route.matched.filter(route => route.meta && route.meta.title);
    breadcrumbs.value = matchedRoutes.map(route => ({
      breadcrumbName: route.meta.title as string,
      path: route.path,
    }));
  },
  { immediate: true }
);
const goBack = () => {
  router.back();
};
</script>

<style scoped>
  .page-header {
    padding: 20px 24px;
    background-color: #fff;
  }
  .page-header-breadcrumb { 
    margin-bottom: 12px;
  }
  .page-header-heading{
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .page-header-heading-left{
    display: flex;
    align-items: center;
    gap: 16px;
  }
  .page-header-title{
    color: rgba(0, 0, 0, 0.88);
    font-weight: 600;
    font-size: 20px;
    line-height: 32px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .page-header-sub-title {
    color: var(--text-secondary);
  }
  .page-header-heading-extra{
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
  .page-header-content {
    padding-top: 12px;
  }
  .page-header-heading-icon {
    font-size: 16px;
  }
  .page-header-heading-icon:hover{
    color: var(--color-primary-hover);
    cursor: pointer;
    transition: var(--transition);
  }
</style>